package main

import (
	jwt "github.com/appleboy/gin-jwt"
	"github.com/gin-contrib/cors"
	"github.com/gin-gonic/gin"
	hC "kjjcgroup_server/httpCtrler"
	"kjjcgroup_server/service"
	"kjjcgroup_server/units"
	"time"
)

func main() {
	_, err := service.GetEngBom()
	units.CheckError(err)
	router := gin.Default()
	kv := "ioa_Stu"
	authMiddleware, err := jwt.New(&jwt.GinJWTMiddleware{
		Realm:         kv, //	def.PJName,
		Key:           []byte(kv),
		Timeout:       time.Hour * 24,
		MaxRefresh:    time.Hour * 24,
		Authenticator: hC.Authenticator,
		PayloadFunc:   hC.PayloadFunc,
		LoginResponse: hC.LoginResponse,
		Authorizator:  hC.Authorizator,
		Unauthorized:  hC.NoJwt,
	})
	units.CheckError(err)
	cfg := cors.DefaultConfig()
	cfg.AllowAllOrigins = true
	cfg.AddAllowHeaders("Token", "Authorization")
	router.Use(cors.New(cfg))

	router.POST("/login", authMiddleware.LoginHandler)
	hC.LogMap["/login"] = "登录"

	routerAuthUrl := router.Group("/")
	routerAuth := routerAuthUrl.Use(authMiddleware.MiddlewareFunc(), hC.LogMiddleware).(*gin.RouterGroup)
	router.Static("/upload", "./upload")

	hC.StartRouter(router, routerAuth)
	hC.UserRouter(router, routerAuth)   // 用户注册登陆管理
	hC.UploadRouter(router, routerAuth) // 文件上传
	hC.NewsRouter(router, routerAuth)
	hC.ProfessionRouter(router, routerAuth)
	hC.CertRouter(router, routerAuth)
	hC.PageRouter(router, routerAuth)
	hC.ServiceRouter(router, routerAuth)
	hC.GuestbookRouter(router, routerAuth)

	err = router.Run("0.0.0.0:9043")
	units.CheckError(err)
}
